package datastructure;
import java.util.*;

import ICTCLAS.I3S.AC.ICTCLAS50;

public class Querystruct {
	static String stopword[] = {"上","中","时候","时","已","有关"};
	public int sort;
	public String require_pos;
	public sentence sent;
	public String tag_content;
	public String content;
	public ArrayList<word> keyword;
	public ArrayList<word> words;
	public ArrayList<String> viword;
	ArrayList<String[]> kinds;
	public String qtag;
	public boolean combine;
	String Pos[] = {"nr","ns","t","m","m","n"}; ;
	public Querystruct(sentence a, boolean c)
	{
		sent= a;
		tag_content = a.tag_content;
		require_pos = "";
		content = a.content;
		words = new ArrayList<word>();
		keyword = new ArrayList<word>();
		viword = new ArrayList<String>();
		sort = 5;
		combine = c;
		qtag = "";
		String kind1[] = {"谁","什么人","哪个人","哪一个人","哪位","哪一位","什么名","原名"};//nr
		String kind2[] = {"什么地方","什么地点","哪里","哪儿","何处","哪个国家","哪国","哪一个国家"
						,"哪个城市","哪一个城市","哪个省","哪一个省","哪个地方","哪个区域"
						,"哪一座","哪座","哪个港口","何国","什么海峡","哪个洲"};//ns
		String kind3[] = {"什么时候","什么时间","哪个时候","何时","哪一天","哪天"
						,"哪月","哪一月","哪个时期"};//t
		String kind4[] = {"多久","多长时间","多少时间","哪一年","哪年"};//m
		String kind5[] = {"多少","几"};//m
		//String kind6[] = {"为什么","什么原因","什么因素"};
		//String kind7[] = {"哪些方法","哪些方式","哪些算法","哪些途径","什么方法","什么方式"};
		kinds = new ArrayList<String[]>();
		kinds.add(kind1);
		kinds.add(kind2);
		kinds.add(kind3);
		kinds.add(kind4);
		kinds.add(kind5);
		parse();
	}
	
	public void addword(word a)
	{
		if(!words.contains(a))
		{
			words.add(a);
		}
	}
	
	public boolean containskeyword(word a)
	{
		for(int i = 0; i < keyword.size(); i++)
		{
			if(keyword.get(i).pos.equals(a.pos) && keyword.get(i).content.equals(a.content))
			{
				return true;
			}
		}
		return false;
	}
	
	public void parse()
	{
		try
		{
			ICTCLAS50 testICTCLAS50 = new ICTCLAS50();
			String argu = ".";
			//初始化
			if (testICTCLAS50.ICTCLAS_Init(argu.getBytes("UTF-8")) == false)
			{
				System.out.println("Init Fail!");
				return;
			}


			//设置词性标注集(0 计算所二级标注集，1 计算所一级标注集，2 北大二级标注集，3 北大一级标注集)
			testICTCLAS50.ICTCLAS_SetPOSmap(0);


			//导入用户词典前分词
			byte nativeBytes[] = testICTCLAS50.ICTCLAS_ParagraphProcess(content.getBytes("UTF-8"), 0, 1);//分词处理
			//System.out.println(nativeBytes.length);
			String nativeStr = new String(nativeBytes, 0, nativeBytes.length, "UTF-8");
			tag_content = nativeStr;
			//System.out.println("未导入用户词典的分词结果： " + nativeStr);//打印结果
			//处理结果转化
			
			extractword();
			judge();
			//for(int i = 0; i < words.size(); i++)
			//{
				//System.out.println(words.get(i).content+" "+words.get(i).pos+" "+words.get(i).position);
			//}			
			testICTCLAS50.ICTCLAS_Exit();
		}
		catch (Exception ex)
		{
			System.out.println("parse "+ex);
		}
	}
	
	public void judge()
	{
		boolean find = false;
		for(int i = 0; i < kinds.size(); i++)
		{
			String temp[] = kinds.get(i);
			for(int j = 0; j < temp.length; j++)
			{
				if(content.indexOf(temp[j]) != -1)
				{
					sort = i;
					require_pos = Pos[i];
					qtag = temp[j];
					find = true;
					//System.out.println(require_pos);
					break;
				}
			}
		}
		if(!find)
			require_pos = Pos[sort];
		return;
	}
	
	public void extractword()
	{
		String tagcontent = this.tag_content;
		String Words[] = tagcontent.split(" ");
		String Str = "";
		boolean iscombine = false;
		int count = 1;
		//word head = new word("","",0);
		for(int i = 0; i < Words.length; i++)
		{
			String part[] = Words[i].split("/");
			if(part.length == 2)
			{
				if(iscombine == false && (part[0].equals("\"") || part[0].equals("“") || part[0].equals("《")))
				{
					iscombine = true;
					continue;
				}
				if(iscombine && (part[0].equals("\"") || part[0].equals("”") || part[0].equals("》")))
				{
					word temp = new word(Str,"n",count);
					count ++;
					
					iscombine = false;
					if(!containskeyword(temp))
						keyword.add(temp);
					viword.add(Str);
					//System.out.println(Str);
					Str = "";
					continue;
				}
				if(iscombine)
				{
					Str += part[0];
				}
				else
				{
					word temp = new word(part[0],part[1],count);
					word last;
					if(iskey(part[1],part[0]))
					{
						
						if(words.size()>=1 && keyword.size()>=1)
						{
							word bottom = words.get(words.size()-1);
							word bottom1 = keyword.get(keyword.size()-1);
							if(bottom.content.equals(bottom1.content))
							{
								char a = bottom.pos.charAt(0);
								char b = temp.pos.charAt(0);
								if((a == b && combine) || (bottom1.content.equals("最") && 
										temp.pos.startsWith("a")))
								{
									last = new word(bottom.content+temp.content,String.valueOf(a),count-1);
									words.remove(words.size()-1);
									words.add(last);
									keyword.remove(keyword.size()-1);
									if(!containskeyword(last))
										keyword.add(last);
									//如果以“最”开头，加入viword
									if(last.content.startsWith("最"))
									{
										if(!viword.contains("最"))
										{
											viword.add("最");
										}
									}
									continue;
								}
								if((bottom.pos.equals("nr1") && temp.pos.equals("nr2"))
										||(bottom.pos.equals("n") && temp.pos.equals("ng")))
								{
									last = new word(bottom.content+temp.content,String.valueOf(a),count-1);
									words.remove(words.size()-1);
									words.add(last);
									keyword.remove(keyword.size()-1);
									if(!containskeyword(last))
										keyword.add(last);
									continue;
								}
								
							}
						}
						//如果以“第”开头，加入viword
						if(temp.content.startsWith("第"))
							viword.add(temp.content);
						if(!containskeyword(temp) && !isremove(temp.content))
							keyword.add(temp);
					}
					addword(temp);
					count ++;
				}
			}
		}
		return;
	}
	
    boolean isremove(String str)
    {
    	for(int i = 0; i < stopword.length; i++)
    	{
    		if(str.equals(stopword[i]))
    		{
    			return true;
    		}
    	}
    	return false;
    }
	
	public boolean iskey(String pos, String str)
	{
		if(pos.startsWith("n") || (pos.startsWith("v") && !pos.equals("vyou")
				&& !pos.equals("vshi")) || pos.startsWith("t") || pos.startsWith("s")
				|| pos.startsWith("a") || pos.startsWith("z") || (pos.startsWith("m") && (str.length()>1) /*&& (!pos.equals("mq"))*/)
				|| pos.equals("r")|| pos.equals("f") || pos.equals("d") || pos.equals("qt"))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
